
package com.gm.wholesale.module.good.ui.mainGood;
import android.content.Context;
import android.util.ArrayMap;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import androidx.annotation.NonNull;
import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BaseListResp;
import com.gm.commonlib.base.baseBean.BasePageData;
import com.gm.commonlib.base.baseBean.DataResp;
import com.gm.commonlib.base.baseui.baseListviewUI.BaseGridFragment;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.network.HttpServicesHolder;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.constant.Constant;
import com.gm.wholesale.module.good.GoodType;
import com.gm.wholesale.module.good.api.IGoodApi;
import com.gm.wholesale.module.good.bean.GoodBean;
import com.gm.wholesale.module.good.bean.GoodListBean;
import com.gm.wholesale.module.good.model.GoodModel;
import com.gm.wholesale.module.main.ui.MainActivity;
import com.gm.wholesale.module.good.view.GoodItemTagView;
import com.gm.wholesale.module.unit.UnitType;
import com.gm.wholesale.util.CloneUtil;
import com.gm.wholesale.util.Utils;

import java.io.IOException;
import java.util.List;

import io.reactivex.Observable;


public class GoodContentFragment extends BaseGridFragment<GoodBean, BasePageData<GoodBean>> {
  private long kindId;
  public void setKindId(long kindId){
      this.kindId=kindId;
  }
   private MainActivity mainActivity;
    @Override
    public void initPullToRefreshListView() {
        super.initPullToRefreshListView();
        mGridView.setNumColumns(GridView.AUTO_FIT);
        setRefreshAndLoadEnable(true,false);
    }

    // todo 获取临时商品
    private void getTemporary(){
        GoodModel.getTemporary(this, new BaseHttpObserver<>() {
            @Override
            protected void handleResultOkResp(DataResp<GoodBean> resp) {
                if (resp!=null && resp.getData()!=null){
                    resp.getData().setTemporaryGood(true);
                    mDatas.add(0,resp.getData());
                    if (mBaseAdapter==null){
                        mBaseAdapter= createTempBaseAdapter();
                    }
                    notifyListDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mainActivity= (MainActivity) context;
    }

    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        super.onCreateContentBody(parent);
        setBodybg(getResources().getColor(R.color.color_E7EDF5));

    }
    public void getDataOnCompleted(){
        super.getDataOnCompleted();
        if (kindId==0){
            //防止下拉加载出现多个
            if (!mDatas.isEmpty() && !mDatas.get(0).isTemporaryGood()){
                getTemporary();
            }

        }

    }


    @Override
    protected BaseAdapter createTempBaseAdapter() {

        return new CommonAdapter<>(getActivity(), mDatas, R.layout.list_item_good) {
            @Override
            public void convert(ViewHolder viewHolder, final GoodBean data) {
                viewHolder.setText(R.id.goodNameTv, data.getName());
                GoodItemTagView goodItemTagView = viewHolder.getView(R.id.textTagView);
                goodItemTagView.setText(data.getName());
                showUnitName(data, goodItemTagView);
                viewHolder.getConvertView().setOnClickListener(view -> {
                    if (mainActivity != null) {
                        try {
                            //todo 克隆一个新的，不然同一个商品输入不同数据，会覆盖第一个
                           GoodBean newGood= CloneUtil.clone(data);
                            mainActivity.showCreateOrderView(newGood);
                        } catch (Exception e) {
                            ToastUtil.showToast("出现异常");
                        }

                    }

                });

            }

            private void showUnitName(GoodListBean goodBean, GoodItemTagView goodItemTagView) {
                int goodType = goodBean.getType();
                //非库存商品,没得单位，默认显示斤
                if (goodType == GoodType.UN_INVENTORY_GOOD) {
                    goodItemTagView.showGreenText("斤");
                } else {
                    List<GoodListBean.UnitsBean> unitsBeans = goodBean.getUnits();
                    if (unitsBeans != null && !unitsBeans.isEmpty()) {
                        for (GoodListBean.UnitsBean unitsBean : unitsBeans) {
                            //仅库存商品生效，1库存单位，2辅助单位
                            if (unitsBean.getType() == UnitType.INVENTORY_UNIT) {
                                String unitName = unitsBean.getName();
                                if (Utils.isWeightUnit(unitName)) {
                                    goodItemTagView.showGreenText(unitName);
                                } else {
                                    goodItemTagView.showRedText(unitName);
                                }
                                break;
                            }
                            else {
                                goodItemTagView.showGreenText("斤");
                            }
                        }
                    }
                    else {
                        goodItemTagView.showGreenText("斤");
                    }
                }

            }
        };
    }

    @Override
    protected HttpServicesHolder<?, BasePageData<GoodBean>> getHttpServicesHolder(long index) {
        return new HttpServicesHolder<IGoodApi, BasePageData<GoodBean>>() {
            @Override
            public Observable<BasePageData<GoodBean>> getHttpObservable(IGoodApi api) {
                ArrayMap<String, Object> quearyData = new ArrayMap<>();
                quearyData.put("categoryId", kindId);
                quearyData.put("pageSize", 35);
                quearyData.put("onShelf",1);
                return api.getGoods(quearyData);
            }
        };
    }

}
